package com.example.audiostreaming.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.nfc.Tag;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.audiostreaming.R;
import com.example.audiostreaming.activity.DetailActivity;
import com.example.audiostreaming.adapters.AdapterAlbumRv;
import com.example.audiostreaming.base.BaseFragment;
import com.example.audiostreaming.implement.ImplDetailLogic;
import com.example.audiostreaming.implement.SubscriptionPresenter;
import com.example.audiostreaming.interfaces.ISubscriptionPresenterCallback;
import com.example.audiostreaming.views.ConfirmDialog;
import com.example.audiostreaming.views.ViewUILoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

import static com.example.audiostreaming.base.BaseApplication.getAppContext;
import static com.example.audiostreaming.utils.UtilConstants.MAX_SUB_COUNT;

/**
 * 订阅片段
 */
public class FragmentSubscription extends BaseFragment {

    private ViewUILoader mUiLoader;
    private SubscriptionPresenter mSubscriptionPresenter;
    private SubscriptionPresenterCallback mSubscriptionPresenterCallback;
    private AdapterAlbumRv mAdapterAlbumRv;
    private Album mCurrentClickAlbum=null;

    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, ViewGroup container) {
        FrameLayout subViewSubscription = (FrameLayout) layoutInflater.inflate(R.layout.fragment_subscription, container, false);//获取订阅子视图
        if (mUiLoader==null) {

            mUiLoader = new ViewUILoader(getAppContext()) {
                @Override
                protected View getSuccessView(ViewGroup container) {
                    return createSussessView(container);
                }

                @Override
                protected View getEmptyView() {
                    View emptyView = LayoutInflater.from(getContext()).inflate(R.layout.view_empty, this, false);
                    TextView emptyViewText = emptyView.findViewById(R.id.empty_view_text);
                    emptyViewText.setText("没有订阅专辑呢，赶紧去订阅吧!");
                    return emptyView;
                }
            };
        }else {
            if(mUiLoader.getParent() instanceof ViewGroup) {
                ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
            }
        }
        mSubscriptionPresenter = SubscriptionPresenter.getInstance();
        mSubscriptionPresenterCallback = new SubscriptionPresenterCallback();
        mSubscriptionPresenter.registerViewCallback(mSubscriptionPresenterCallback);

        mSubscriptionPresenter.getSubscriptionList();
        subViewSubscription.addView(mUiLoader);
        return subViewSubscription;
    }

    private View createSussessView(ViewGroup container) {
        View successView = LayoutInflater.from(container.getContext()).inflate(R.layout.fragment_subscription_item, container, false);
        TwinklingRefreshLayout refreshLayout = successView.findViewById(R.id.over_scroll_view);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableOverScroll(true);

        RecyclerView subscriptionList = successView.findViewById(R.id.subscription_list);
        subscriptionList.setLayoutManager(new LinearLayoutManager(container.getContext()));
        subscriptionList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.top = UIUtil.dip2px(view.getContext(),0);
                outRect.bottom = UIUtil.dip2px(view.getContext(),3);
                outRect.left = UIUtil.dip2px(view.getContext(),3);
                outRect.right = UIUtil.dip2px(view.getContext(),3);
            }
        });

        mAdapterAlbumRv = new AdapterAlbumRv();
        mAdapterAlbumRv.setOnAlbumItemClickListener(new AdapterAlbumRv.OnAlbumItemClickListener() {
            @Override
            public void onAlbumItemClick(int position, Album album) {
                ImplDetailLogic.getInstanceImplDetailLogic().setDetailAlbum(album);
                Intent intent = new Intent(getContext(), DetailActivity.class);
                startActivity(intent);
            }
        });
        mAdapterAlbumRv.setOnAlbumItemLongClickListener(new AdapterAlbumRv.OnAlbumItemLongClickListener() {
            @Override
            public void onAlbumItemLongClick(int position, Album album) {
                mCurrentClickAlbum = album;
                //Toast.makeText(getAppContext(),album.getAlbumTitle()+"  被长按了",Toast.LENGTH_SHORT).show();
                ConfirmDialog confirmDialog = new ConfirmDialog(getActivity());
                confirmDialog.setOnDialogActionClickListener(new ConfirmDialog.OnDialogActionClickListener() {
                    @Override
                    public void onCancelSubClick() {
                        //取消订阅
                        if (mCurrentClickAlbum != null && mSubscriptionPresenter != null) {
                            mSubscriptionPresenter.deleteSubscription(mCurrentClickAlbum);
                        }

                    }

                    @Override
                    public void onGiveUpClick() {
                        //放弃取消订阅
                    }
                });
                confirmDialog.show();
            }
        });
        subscriptionList.setAdapter(mAdapterAlbumRv);

        return successView;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSubscriptionPresenter != null) {
            mSubscriptionPresenter.unRegisterViewCallback(mSubscriptionPresenterCallback);
            mSubscriptionPresenter = null;
        }
    }



    private class SubscriptionPresenterCallback implements ISubscriptionPresenterCallback{
        @Override
        public void onAddResult(boolean isSuccess) {

        }

        @Override
        public void onDeleteResult(boolean isSuccess) {
            Toast.makeText(getAppContext(), isSuccess?"删除成功":"删除失败" ,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSubscriptionsLoaded(List<Album> albums) {
            if (albums==null||albums.size()==0) {
                mUiLoader.upDateStatus(ViewUILoader.UIStatus.EMPTY);
            }else {
                //更新数据
                mAdapterAlbumRv.setData(albums);
                mUiLoader.upDateStatus(ViewUILoader.UIStatus.SUCCESS);
            }
        }

        @Override
        public void onSubFull() {
            Toast.makeText(getAppContext(),"订阅数量不得超过"+MAX_SUB_COUNT,Toast.LENGTH_SHORT).show();
        }
    }
}
